{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# 基于线性回归的月度预算消费比例预测(多标签)\n",
    "import numpy as np\n",
    "import pandas\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn.ensemble import RandomForestRegressor\n",
    "from utils.mulanbay import get_datasetsPath"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.662928Z",
     "start_time": "2023-08-09T03:14:28.635508Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "#读取csv文件\n",
    "data_file = get_datasetsPath()+\"/\"+\"budget_consume_y_m.csv\"\n",
    "data_df = pandas.read_csv(data_file)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.706496Z",
     "start_time": "2023-08-09T03:14:28.642064Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 36966 entries, 0 to 36965\n",
      "Data columns (total 4 columns):\n",
      " #   Column    Non-Null Count  Dtype  \n",
      "---  ------    --------------  -----  \n",
      " 0   score     36966 non-null  int64  \n",
      " 1   dayIndex  36966 non-null  int64  \n",
      " 2   rate1     36966 non-null  float64\n",
      " 3   rate2     36966 non-null  float64\n",
      "dtypes: float64(2), int64(2)\n",
      "memory usage: 1.1 MB\n"
     ]
    }
   ],
   "source": [
    "#查看数据集\n",
    "data_df.info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.707289Z",
     "start_time": "2023-08-09T03:14:28.675017Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "#划分data和target\n",
    "data_X = data_df[[\"score\",\"dayIndex\"]]\n",
    "data_y = data_df[[\"rate1\",\"rate2\"]]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.715642Z",
     "start_time": "2023-08-09T03:14:28.702098Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "       score  dayIndex\n0          0         1\n1          0         2\n2          0         3\n3          0         4\n4          0         5\n...      ...       ...\n36961    100       362\n36962    100       363\n36963    100       364\n36964    100       365\n36965    100       366\n\n[36966 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>dayIndex</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>36961</th>\n      <td>100</td>\n      <td>362</td>\n    </tr>\n    <tr>\n      <th>36962</th>\n      <td>100</td>\n      <td>363</td>\n    </tr>\n    <tr>\n      <th>36963</th>\n      <td>100</td>\n      <td>364</td>\n    </tr>\n    <tr>\n      <th>36964</th>\n      <td>100</td>\n      <td>365</td>\n    </tr>\n    <tr>\n      <th>36965</th>\n      <td>100</td>\n      <td>366</td>\n    </tr>\n  </tbody>\n</table>\n<p>36966 rows × 2 columns</p>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_X"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.780194Z",
     "start_time": "2023-08-09T03:14:28.721100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "#通过随机森林回归来填补缺失值\n",
    "X_missing_reg = data_X.copy();"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.780714Z",
     "start_time": "2023-08-09T03:14:28.736241Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "array([], dtype=object)"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#计算为空的列\n",
    "X_df = X_missing_reg.isnull().sum()\n",
    "# 得出列名 缺失值最少的列名 到 缺失值最多的列名\n",
    "colName = X_df[~X_df.isin([0])].sort_values().index.values\n",
    "colName"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.781531Z",
     "start_time": "2023-08-09T03:14:28.747060Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "from utils.mulanbay import fill_missing_rf\n",
    "#填补\n",
    "#选择标签的第一列为训练用的值\n",
    "fill_y = data_y.iloc[:,0]\n",
    "#使用列名进行索引填补\n",
    "for cn in colName:\n",
    "    y_pred = fill_missing_rf(X_missing_reg,fill_y,cn)\n",
    "    X_missing_reg.loc[X_missing_reg.loc[:,cn].isnull(),cn] = y_pred"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "       score  dayIndex\n0          0         1\n1          0         2\n2          0         3\n3          0         4\n4          0         5\n...      ...       ...\n36961    100       362\n36962    100       363\n36963    100       364\n36964    100       365\n36965    100       366\n\n[36966 rows x 2 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>score</th>\n      <th>dayIndex</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>36961</th>\n      <td>100</td>\n      <td>362</td>\n    </tr>\n    <tr>\n      <th>36962</th>\n      <td>100</td>\n      <td>363</td>\n    </tr>\n    <tr>\n      <th>36963</th>\n      <td>100</td>\n      <td>364</td>\n    </tr>\n    <tr>\n      <th>36964</th>\n      <td>100</td>\n      <td>365</td>\n    </tr>\n    <tr>\n      <th>36965</th>\n      <td>100</td>\n      <td>366</td>\n    </tr>\n  </tbody>\n</table>\n<p>36966 rows × 2 columns</p>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_missing_reg"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.853893Z",
     "start_time": "2023-08-09T03:14:28.785557Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "array([0, 0])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res = np.sort(X_missing_reg.isnull().sum(axis=0))\n",
    "res"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:28.854580Z",
     "start_time": "2023-08-09T03:14:28.795376Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "-0.0010098594073058868"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#交叉验证\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.model_selection import cross_val_score\n",
    "reg = LinearRegression().fit(X_missing_reg, data_y)\n",
    "scores = cross_val_score(reg,X_missing_reg,data_y,scoring='neg_mean_squared_error',cv=5).mean()\n",
    "# NEG_MSE\n",
    "scores"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:29.017793Z",
     "start_time": "2023-08-09T03:14:28.807044Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [],
   "source": [
    "data_X = X_missing_reg"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:29.018197Z",
     "start_time": "2023-08-09T03:14:28.932481Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [],
   "source": [
    "#生成模型文件\n",
    "from sklearn_pandas import DataFrameMapper\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.feature_selection import SelectKBest\n",
    "from sklearn.impute import SimpleImputer\n",
    "from sklearn2pmml.decoration import ContinuousDomain\n",
    "from sklearn2pmml.pipeline import PMMLPipeline\n",
    "from sklearn.multioutput import MultiOutputRegressor\n",
    "\n",
    "#数据预处理\n",
    "mapper = DataFrameMapper([\n",
    "\t\t([\"score\", \"dayIndex\"], [ContinuousDomain(), SimpleImputer()])\n",
    "\t    ])\n",
    "#算法\n",
    "#参考：https://scikit-learn.org.cn/view/91.html\n",
    "clf = MultiOutputRegressor(LinearRegression())\n",
    "\n",
    "pipeline = PMMLPipeline([\n",
    "\t(\"mapper\", mapper),\n",
    "\t(\"classifier\", clf)\n",
    "])\n",
    "\n",
    "pipeline.active_fields = [\"score\", \"dayIndex\"]\n",
    "pipeline.target_fields = [\"rate1\",\"rate2\"]\n",
    "\n",
    "pipeline.fit(data_X, data_y)\n",
    "pipeline.verify(data_X.sample(n = 15))\n",
    "\n",
    "from sklearn2pmml import sklearn2pmml\n",
    "from utils.mulanbay import get_modulePath\n",
    "module_file = get_modulePath()+\"/\"+\"LinearRegressor_budget_consume_y_m.pmml\"\n",
    "sklearn2pmml(pipeline, module_file,with_repr = True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-08-09T03:14:31.576915Z",
     "start_time": "2023-08-09T03:14:28.946533Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
